1466C - Canine poetry - CodeForces Solution


dp greedy strings *1300

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    s = list(input())
    c = 0
    for i in range(1, len(s)):
        if i >= 2 and s[i] == s[i - 2] or s[i] == s[i - 1]:
            c += 1
            s[i] = '@'
    print(c)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define DIVYA                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define ll long long
#define cy cout << "YES\n"
#define cn cout << "NO\n"
typedef vector<ll> vll;
#define pb push_back
#define inf 1000000000000000005
const ll mod = 1e9 + 7;
ll inv(ll i)
{
    if (i == 1)
        return 1;
    return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;
}
ll mod_mul(ll a, ll b)
{
    a = a % mod;
    b = b % mod;
    return (((a * b) % mod) + mod) % mod;
}
ll mod_add(ll a, ll b)
{
    a = a % mod;
    b = b % mod;
    return (((a + b) % mod) + mod) % mod;
}
ll ceil_div(ll a, ll b) { return a % b == 0 ? a / b : a / b + 1; }
ll bin_expo(int a, int b, int m)
{
    int res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = (res * a) % m;
        a = (a * a) % m;
        b = b >> 1;
    }
    return res;
}
ll mod_inv(int a, int b) { return bin_expo(a, b - 2, b); }
//****************************Template Ends*******************************//

int main()
{
    DIVYA;
    ll t, n,m, i, j, ans,temp, sum,pos = 0,check = 1,x,y,cnt = 0;
    string s;
    t = 1;
    cin >> t;
    while (t--)
    {
         ans = temp = sum = 0;
         cin >> s;
         n = s.size();
         for(int i = 0; i < n; i++)
         {
             if(s[i] == s[i+1] and s[i] != '#' and i + 1 < n)
             {
                 ans++;
                 s[i+1] = '#';
             }
             if(s[i+2] == s[i] and s[i] != '#' and i + 2 < n)
             {
                 ans++;
                 s[i+2] = '#';
             }
         }
         cout << ans << "\n";
         
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus